<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validate-trigger="submit" err-show-type="toast">
			<uni-forms-item name="title" label="任务标题" required>
				<uni-easyinput placeholder="一句话总结任务" v-model="formData.title" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="price" label="任务奖金" required>
				<uni-easyinput placeholder="以【分】为单位哦" type="number" v-model="formData.price"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="deadline" label="调研截止时间" required>
				<uni-datetime-picker return-type="timestamp" v-model="formData.deadline"></uni-datetime-picker>
			</uni-forms-item>
			<uni-forms-item name="task_location" label="调研地" required>
				<uni-data-picker self-field="code" parent-field="parent_code" v-model="formData.task_location"
					collection="opendb-city-china" orderby="value asc"
					field="code as value, name as text, eq(type, 2) as isleaf"></uni-data-picker>
			</uni-forms-item>
			<uni-forms-item name="task_desc" label="详细描述" required>
				<uni-easyinput type="textarea" placeholder="请详细描述任务要求。最好分点罗列要求。如：
1.
2.
3.
..." v-model="formData.task_desc" trim="both"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/tasks.js';

	const db = uniCloud.database();
	const dbCollectionName = 'tasks';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}

	export default {
		data() {
			let formData = {
				"title": "",
				"price": null,
				"deadline": null,
				"task_location": "",
				"task_desc": ""
			}
			return {
				formData,
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
		padding: 12px 20px;
		font-size: 14px;
		border-radius: 4px;
		line-height: 1;
		margin: 0;
	}
</style>
